package com.htthorizon.powersupply.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htthorizon.powersupply.domain.AdsGridCjbdLineLoadFlowDf;
import com.htthorizon.powersupply.domain.LineLoadMonitor;
import com.htthorizon.powersupply.dto.LineLoad;
import com.htthorizon.powersupply.mapper.LineLoadMonitorMapper;
import com.htthorizon.powersupply.service.LineLoadMonitorService;
import com.htthorizon.powersupply.utils.OrgUtil;
import com.htthorizon.powersupply.vo.LineLoadMonitorVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Brilliant
 * @description 针对表【line_load_monitor】的数据库操作Service实现
 * @createDate 2025-01-22 10:27:45
 */
@Service
@Slf4j
public class LineLoadMonitorServiceImpl extends ServiceImpl<LineLoadMonitorMapper, LineLoadMonitor> implements LineLoadMonitorService {


    @Autowired
    private AdsGridCjbdLineLoadFlowDfServiceImpl adsGridCjbdLineLoadFlowDfService;

    private Map<String, AdsGridCjbdLineLoadFlowDf> map = new HashMap<>();

    @Override
    public void compute(List<LineLoad> lineLoads) {
        String date = DateUtil.format(DateUtil.date(), "yyyyMMdd");
        List<LineLoadMonitor> results = new ArrayList<>(lineLoads.size());
        log.info("开始计算线路重过载");
        List<AdsGridCjbdLineLoadFlowDf> list = adsGridCjbdLineLoadFlowDfService.list();
        for (AdsGridCjbdLineLoadFlowDf df : list) {
            map.put(df.getDistElecAutomationPmsId(), df);
        }
        log.info("AdsGridCjbdLineLoadFlowDf size:{}", map.size());
        log.info("LineLoad size:{}", lineLoads.size());
//        log.info("样例 size:{}", lineLoads.get(0));
        for (LineLoad lineLoad : lineLoads) {
            try {
                LineLoadMonitor lineLoadMonitor = new LineLoadMonitor();
                AdsGridCjbdLineLoadFlowDf df = map.get(lineLoad.getLineId());
                if (df == null) continue;
                lineLoadMonitor.setLineId(df.getLineId());
                lineLoadMonitor.setDate(date);
                lineLoadMonitor.setApperTime(lineLoad.getTimestamp());
                lineLoadMonitor.setStdCountyOrgNm(OrgUtil.map(df.getStdContyOrgNm()));
                lineLoadMonitor.setStdCountyOrgNo(df.getStdContyOrgNo());
                lineLoadMonitor.setLineMax(df.getMaxAllowElec());
                lineLoadMonitor.setLineCap(df.getLineLoadFlow());
                lineLoadMonitor.setStdOrgNm(df.getStdTgOrgNm());
                lineLoadMonitor.setStdOrgNo(df.getStdTgOrgNo());
                lineLoadMonitor.setLineNm(df.getLineNm());

                double v = Double.parseDouble(df.getLineLoadFlow());
//                double p = Double.parseDouble(lineLoad.getP()) * 1000; // 有功
//                double up = Double.parseDouble(lineLoad.getWgPower()) * 1000; // 无功
//                double r1 = pow(p, 2) + pow(up, 2);
//                double result = sqrt(r1) / v;
                double a = 0.0;
                double b = 0.0;
                double c = 0.0;
                try {
                    a = Double.parseDouble(lineLoad.getIa());
                } catch (Exception e) {
                    a = 0.0;
                }
                try {
                    b = Double.parseDouble(lineLoad.getIb());
                } catch (Exception e) {
                    b = 0.0;
                }
                try {
                    c = Double.parseDouble(lineLoad.getIc());
                } catch (Exception e) {
                    c = 0.0;
                }

                double r = max(a, b, c);

                double result = r / v;
                if (result > 1) {
                    lineLoadMonitor.setLoadFlow(NumberUtil.round(result * 100, 2) + "%");
                    lineLoadMonitor.setLoadType("过载");
                } else if (result > 0.7) {
                    lineLoadMonitor.setLoadFlow(NumberUtil.round(result * 100, 2) + "%");
                    lineLoadMonitor.setLoadType("重载");
                } else {
                    lineLoadMonitor.setLoadFlow(NumberUtil.round(result * 100, 2) + "%");
                    lineLoadMonitor.setLoadType("正常");
                }
                results.add(lineLoadMonitor);
            } catch (Exception e) {
                // 可能存在没有的
//                    log.error("计算线路重过载失败",e);
            }
        }

        if (!results.isEmpty()) {
            this.saveBatch(results);
        }
        map.clear();


    }

    public static void main(String[] args) {
        System.out.println("max(1.1,2.5,3.3) = " + max(10.1, 7.5, 3.3));
    }

    private static double max(double a, double b, double c) {
        return Math.max(a, Math.max(b, c));
    }


    @Override
    public List<LineLoadMonitorVo> getLoadDataToDay() {
        String today = DateUtil.format(DateUtil.date(), "yyyyMMdd");
        return baseMapper.getLoadDataToDay(today);
    }

    @Override
    public List<LineLoadMonitorVo> getRealTimeLoadData() {
        return baseMapper.getRealTimeLoadData();
    }

    @Override
    public List<LineLoadMonitorVo> getAll() {
        return baseMapper.getAll();
    }
}




